Answer the following questions and complete the exercises in
RMarkdown. Please embed all of your code and push your final work to
your repository. Your final lab report should be organized, clean, and
run free from errors. Remember, you must remove the # for
the included code chunks to run. Be sure to add your name to the author
header above. For any included plots, make sure they are clearly
labeled. You are free to use any plot type that you feel best
communicates the results of your analysis.
Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!
library(tidyverse)
library(janitor)
library(ggmap)
library(leaflet)
register_stadiamaps("11a0cc30-8312-45a9-b18f-959a28e10ed1", write = FALSE)
For this homework, we will use the shark attack data to visualize where the attacks occurred. The data are from: State of California- Shark Incident Database.
sharks <- read_csv("data/SharkIncidents_1950_2022_220302.csv") %>%
clean_names() %>%
filter(longitude !="NA" & latitude !="NA") %>% # pulling out NA locations
mutate(longitude = as.numeric(longitude)) # converting longitude to numeric
sharks %>%
select(longitude, latitude) %>%
summary()
## longitude latitude
## Min. :-124.7 Min. :32.59
## 1st Qu.:-123.1 1st Qu.:34.04
## Median :-122.0 Median :36.70
## Mean :-121.4 Mean :36.36
## 3rd Qu.:-119.6 3rd Qu.:38.18
## Max. :-117.1 Max. :41.56
lat <- c(32.59, 41.56)
long <- c(-124.7, -117.1)
bbox <- make_bbox(long, lat, f=0.03)
stamen in a terrain style projection
and display the map.map <- get_stadiamap(bbox, maptype="stamen_terrain", zoom=7)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(map)
ggmap(map) +
geom_point(data=sharks, aes(longitude, latitude), size=0.75, color="blue", alpha=0.8)+
labs(x="Longitude", y="Latitude", title="CA Shark Attacks")
sharks_fat <- sharks %>%
filter(injury=="fatal")
ggmap(map) +
geom_point(data=sharks_fat, aes(longitude, latitude), size=0.75, color="red", alpha=0.8)+
labs(x="Longitude", y="Latitude", title="CA Shark Attacks")
leaflet. Also, color the points red.leaflet(sharks_fat) %>%
addProviderTiles(providers$Stadia.StamenTerrain) %>%
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 2,
stroke = FALSE,
fillOpacity = 0.7,
color = "red"
) %>%
addScaleBar(position = "bottomleft") %>%
fitBounds(lng1=-124.7, lat1=32.59,
lng2=-117.1, lat2 = 41.56)
sharks %>%
group_by(county) %>%
summarize(tot=n()) %>%
slice_max(tot,n=1)
## # A tibble: 1 × 2
## county tot
## <chr> <int>
## 1 San Diego 24
sharks %>%
filter(county=="San Diego") %>%
select(longitude, latitude) %>%
summary()
## longitude latitude
## Min. :-118.1 Min. :32.59
## 1st Qu.:-117.6 1st Qu.:32.77
## Median :-117.4 Median :32.85
## Mean :-117.5 Mean :32.97
## 3rd Qu.:-117.3 3rd Qu.:33.23
## Max. :-117.1 Max. :33.37
lat <- c(32.59, 33.37)
long <- c(-118.1, -117.1)
bbox <- make_bbox(long, lat, f=0.03)
map2 <- get_stadiamap(bbox, maptype="stamen_terrain", zoom=8)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
sharks_SD <- sharks %>%
filter(county=="San Diego")
ggmap(map2) +
geom_point(data=sharks_SD, aes(longitude, latitude), size=0.75, color="blue", alpha=0.8)+
labs(x="Longitude", y="Latitude", title="San Diego Shark Attacks")
Please knit your work as an .html file and upload to Canvas. Homework is due before the start of the next lab. No late work is accepted. Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!